using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Common;	
using SystemFrameworks;
using Microsoft.ApplicationBlocks.Data;    
namespace MinCultura.Sipa.AgentesArtes
{
	/// <summary>
	/// Clase de acceso a datos para la entidad ART_AREAS_ARTISTICAS_TEMAS
	/// </summary>
	public class AreasArtisticasTemas 
	{

        #region propiedades

        private string _cadenaConexion;

        public string ConnectionString
        {
            get { return _cadenaConexion; }
            set { _cadenaConexion = value; }
        }

        #endregion

    #region Private Data Access Atributes
    private const String ARD_ID_PARAM = "@ARD_ID";
    private const String AAT_ID_PARAM = "@AAT_ID";
    private const String AAT_TEMA_PARAM = "@AAT_TEMA";
	#endregion Private Data Access Atributes
  
	/// <summary>
	/// Constructor de la clase ARTAREASARTISTICASTEMAS
	/// </summary>
        public AreasArtisticasTemas(string strCadenaConecxion)
        {
            ConnectionString = strCadenaConecxion;
        }


	public MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData BuscarAreasArtisticasTemas()
	{
	MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData response = new MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData();
	SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_BUSCAR_ART_AREAS_ARTISTICAS_TEMAS", response.Tables[0]);
		if (response.Tables[0].Rows.Count == 0)
			return null;
		return response;
	}

		public MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData BuscarAreasArtisticasTemas(decimal ARDID)
		{
			MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData response = new MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData();
			SqlParameter param1 = new SqlParameter(ARD_ID_PARAM, ARDID);
		
			SqlParameter[] parameters = {param1};		

			SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_AREAS_ARTISTICAS_TEMAS_TEMA", response.Tables[0],parameters);
			if (response.Tables[0].Rows.Count == 0)
				return null;
			return response;
		}
	
	
	public MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData ConsultarAreasArtisticasTemas	(decimal parAAT_ID)
	{
	MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData response = new MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData();
	
		SqlParameter param1 = new SqlParameter(AAT_ID_PARAM, parAAT_ID);
		
		SqlParameter[] parameters = {param1};		
		SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_AREAS_ARTISTICAS_TEMAS", response.Tables[0], parameters);
		if (response.Tables[0].Rows.Count == 0)
			return null;
		return response;
	}
			
	public decimal  AdicionarAreasArtisticasTemas (MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData datos)		 									
		
	{
		DataTable tabla = datos.Tables[MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData.ART_AREAS_ARTISTICAS_TEMAS_TABLE];
		decimal decAAT_ID=0;
				
		try
		{	
			//StartTransaction();

			foreach (DataRow row in tabla.Rows)
			{				
				SqlParameter param1 = new SqlParameter(ARD_ID_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData.ARD_ID_FIELD]);
				SqlParameter param2 = new SqlParameter(AAT_ID_PARAM, SqlDbType.Decimal);
				param2.Direction=ParameterDirection.Output;
				SqlParameter param3 = new SqlParameter(AAT_TEMA_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData.AAT_TEMA_FIELD]);					
				SqlParameter[] parameters = {param1,param2,param3};
				SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ADICIONAR_ART_AREAS_ARTISTICAS_TEMAS", parameters); 				
				decAAT_ID=decimal.Parse(param2.Value.ToString());				
			} 
			
            //if (this.RegistroAuditoria!=null)
            //{
            //    this.RegistroAuditoria.Rows[0][RegistroAuditoriaData.REG_IDENTIFICADOR_FIELD] = decAAT_ID;
            //}

            //RegistrarAuditoria();

            //CompleteTransaction();

			return(decAAT_ID);			 
		} 		
		catch (SqlException sqlEx)
		{	
			//AbortTransaction();
			throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREAS_ARTISTICAS_TEMAS"),sqlEx); 
		}     
		catch (Exception ex) 
		{ 
			//AbortTransaction();
			throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorInsertar", "ART_AREAS_ARTISTICAS_TEMAS"),ex); 
		}	
	}		
	
	public void ActualizarAreasArtisticasTemas (MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData datos)
	{
		DataTable tabla = datos.Tables[MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData.ART_AREAS_ARTISTICAS_TEMAS_TABLE];
		MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData areasArtisticasTemasDataAnt = new MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData();

		try
		{
            //StartTransaction();

            //if (this.RegistroAuditoria!=null)
            //{
            //    areasArtisticasTemasDataAnt = ConsultarAreasArtisticasTemas(Convert.ToDecimal(tabla.Rows[0][MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData.AAT_ID_FIELD]));
            //}

			foreach (DataRow row in tabla.Rows)
			{			
				SqlParameter param1 = new SqlParameter(ARD_ID_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData.ARD_ID_FIELD]);
				SqlParameter param2 = new SqlParameter(AAT_ID_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData.AAT_ID_FIELD]);
				SqlParameter param3 = new SqlParameter(AAT_TEMA_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.AreasArtisticasTemasData.AAT_TEMA_FIELD]);
					
				SqlParameter[] parameters = {param1,param2,param3};
				SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ACTUALIZAR_ART_AREAS_ARTISTICAS_TEMAS", parameters); 
			}
 
            //RegistrarAuditoria(areasArtisticasTemasDataAnt.Tables[0],tabla);

            //CompleteTransaction();
		} 
		catch (SqlException sqlEx)
		{		
			//AbortTransaction();
			throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREAS_ARTISTICAS_TEMAS"),sqlEx); 
		}     
		catch (Exception ex) 
		{ 
			//AbortTransaction();
			throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorActualizar", "ART_AREAS_ARTISTICAS_TEMAS"),ex); 
		}
	} 
	
		/// <summary>
		/// Elimina un registro de la entidad Artareasartisticastemas
		/// </summary>
		/// <param name="parAAT_ID">Identificador del registro
		public void EliminarAreasArtisticasTemas (decimal parAAT_ID)
		{
			try
			{
				SqlParameter param2 = new SqlParameter(AAT_ID_PARAM, parAAT_ID);
					
				SqlParameter[] parameters = {param2};
				SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREAS_ARTISTICAS_TEMAS", parameters); 
			}
			catch (SqlException sqlEx)
			{		
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREAS_ARTISTICAS_TEMAS"),sqlEx); 
			}     
			catch (Exception ex) 
			{ 
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_AREAS_ARTISTICAS_TEMAS"),ex); 
			}
		}
		
		public void EliminarAreasArtisticasTemas(ArrayList arrIDs)
    {
			try
			{
				//StartTransaction();

				foreach(object Item in arrIDs)
				{		
					SqlParameter param2 = new SqlParameter(AAT_ID_PARAM, decimal.Parse(Item.ToString()));
					SqlParameter[] parameters = {param2};
					SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREAS_ARTISTICAS_TEMAS", parameters); 
				}

                //RegistrarAuditoria();

                //CompleteTransaction();
			}
			catch (SqlException sqlEx)
			{
				//AbortTransaction();
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREAS_ARTISTICAS_TEMAS"),sqlEx); 
			}
			catch (Exception ex)
			{
				//AbortTransaction();
				throw new ApplicationExceptionManager (Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_AREAS_ARTISTICAS_TEMAS"),ex); 
			}				  					 
    }
		
	}	
}		

